<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Site/classes/PhpFox_ComponentEditBannedIps.class.php, $Author:  $ */



/** Component for editing ban ips list
 * @version $Id: PhpFox_ComponentEditBannedIps.class.php 233 2008-01-27 01:39:11Z phpfoxsv $
 * @package module.site
 *
 */
class PhpFox_Mod_Site_ComponentEditBannedIps extends BaseComponent
{
    function process()
    {
        $oReq = &App::getRequest();
        $oUrl = &App::getUrl();
        $oTpl = &$this->_oModule->getTemplate();
        $oAdminService = &App::getModuleService('Admin', 'Admin');
        /* @var $oAdminService PhpFox_Mod_Admin_ServiceAdmin */
        $oIPItem = &$this->_oModule->getItem('IP');
        /* @var $oIPItem PhpFox_Mod_Site_ItemIP */
        App::loadClass('Validator');
        App::loadClass('Pager');
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        if (!$oSecurityService->hasStaffPerm('MENU_deny'))
        {
            App::gotoUrl('admin.main');
        }

        $iPage = $oReq->getInt('page', 1);
        $iPageSize = App::getParam('admin.banipsize');
        $aFields = array
        (
            'val[ip]' => array('title' => App::format('core.ip.address'), 'def' => 'ip'),
        );
        $oValidator = &new Validator($aFields);
        $aPeriods = App::getParam('block_periods');
        if ($oReq->getAction() == 'unban')
        {
            $iIpId = $oReq->getInt('id');
            $oUrl->clearParams('id');
            if ($iIpId && $oIPItem->load($iIpId))
            {
                $oIPItem->delete($iIpId);
                App::rmCache('banIp.php');
                App::forward($oUrl->getUrl(), App::format('Mod_Site.info.ip_deleted'), 'message', '', 'ipadd');
            }
            App::forward($oUrl->getUrl());
        }
        if ($oReq->getAction() == 'add')
        {
            $aErrors = array();
            $iIPId = $oReq->getInt('id');
            $aValues = $oReq->getArray('val');
            if (!isset($aPeriods[$aValues['period']]))
            {
                App::gotoUrl('admin.extra.deny', array('page' => $iPage));
            }
            if (!$oValidator->isValid($oReq->getAll()))
            {
                $this->addErrors($oValidator->getErrors());
            }
            if (!count($this->getErrors()))
            {
                $oIPItem->aData['ip']          = $aValues['ip'];
                $oIPItem->aData['description'] = $aPeriods[$aValues['period']]['description'];
                $oIPItem->aData['atime']       = Database::date();
                $oIPItem->aData['block_until'] = isset($aPeriods[$aValues['period']]['period']) ? Database::date(strtotime($aPeriods[$aValues['period']]['period'])) : '';
                $oIPItem->insert();
                App::rmCache('banIp.php');
                App::gotoUrl('admin.ban', array('type' => 'ip'));
            }
            $oTpl->assign(array(
                'aValues' => $aValues,
                'sIp'     => $sIp,
            ));
        }
        $sSort = 'atime DESC';
        list($aIps, $iCnt) = $oIPItem->getList(array(), $iPage, $iPageSize, $sSort);
        $oPager = &new Pager($iCnt, $iPage, $iPageSize);
        $aPeriodsHash = array();
        foreach ($aPeriods as $iKey => $aPeriod)
        {
            $aPeriodsHash[$iKey] = $aPeriod['description'];
        }

        $oTpl->assignSrc('sValidatorJs', $oValidator->makeJS());
        $oTpl->assignSrc('sValidatorMess', $oValidator->makeJsMess());
        $oTpl->assignSrc('aErrorsIp', $this->getErrors());
        $oTpl->assign(array(
            'aIps'        => $aIps,
            'aPeriods'    => $aPeriodsHash,
            'aPaging'     => ($iCnt) ? $oPager->getInfo($oUrl) : '',
        ));
        return $oTpl->fetch('IPBan.html');
    }
}
?>